using System;
using System.Drawing;
using System.Runtime.Serialization;

namespace Motorway_Simulator.Components
{
    /// <summary>
    /// Bridge road unit component
    /// </summary>
    [Serializable()]
    public class Bridge : RoadUnit, ISerializable
    {
        /// <summary>
        /// Unit icon to display bridge image on the road unit list view
        /// </summary>
        public static Bitmap unitIcon = Motorway_Simulator.Properties.Resources.Bridge;

        /// <summary>
        /// Constructor to initialize Bridge road unit
        /// </summary>
        /// <param name="initX">X position of road unit</param>
        /// <param name="initY">Y position of road unit</param>
        /// <param name="direction">Direction of road unit</param>
        /// <param name="zoom">Initial Zoom level</param>
        public Bridge(int initX, int initY, Direction direction, float zoom) : base()
        {
            position.X = initX;
            position.Y = initY;
            this.direction = direction;
            RoadImage = Motorway_Simulator.Properties.Resources.Bridge;
            ImageZoom = zoom;
            RoadImage = RotateImageClockWise(image);

        }

        /// <summary>
        /// Method which draws road unit at specified position and size
        /// </summary>
        /// <param name="g"></param>
        public override void Draw(Graphics g)
        {
            g.DrawImage(image, position.X, position.Y, image.Width, image.Height);
        }

        /// <summary>
        /// Gets the next point
        /// </summary>
        /// <param name="position"></param>
        /// <param name="speed"></param>
        /// <returns></returns>
        public override Point getNextPosition(Point position, int speed)
        {
            int x = position.X;
            int y = position.Y;
            int newX = 0;
            int newY = 0;
            if (direction == Direction.NORTH)
            {
                newY = y - speed;
                newX = x;
            }
            if (direction == Direction.SOUTH)
            {
                newY = y + speed;
                newX = x;
            }
            if (direction == Direction.EAST)
            {
                newY = y;
                newX = x + speed;
            }
            if (direction == Direction.WEST)
            {
                newY = y;
                newX = x - speed;
            }
            return new Point(newX, newY);
        }

        #region ISerializable Members
        /// <summary>
        /// Add value for serialization
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Position", position);
            info.AddValue("Image", image);
            info.AddValue("ImageWidth", ImageWidth);
            info.AddValue("ImageHeight", ImageHeight);
            info.AddValue("RoadImage", roadImage);
            info.AddValue("ImageZoom", imageZoom);
            info.AddValue("Sign", sign);
            info.AddValue("HasSign", hasSign);
            info.AddValue("Direction", direction);
        }

        #endregion
        /// <summary>
        /// Is called to set values read from file
        /// </summary>
        /// <param name="info">Info</param>
        /// <param name="ctxt">ctxt</param>
        public Bridge(SerializationInfo info, StreamingContext ctxt)
        {
            position = (Point)info.GetValue("Position", typeof(Point));
            image = (Bitmap)info.GetValue("Image", typeof(Bitmap));
            ImageWidth = (float)info.GetValue("ImageWidth", typeof(float));
            ImageHeight = (float)info.GetValue("ImageHeight", typeof(float));
            roadImage = (Bitmap)info.GetValue("RoadImage", typeof(Bitmap));
            imageZoom = (float)info.GetValue("ImageZoom", typeof(float));
            sign = (Sign)info.GetValue("Sign", typeof(Sign));
            hasSign = (bool)info.GetValue("HasSign", typeof(bool));
            direction = (Direction)info.GetValue("Direction", typeof(Direction));
            //Get the values from info and assign them to the appropriate properties
        }
    }
}